import request from '@/router/axios';
import Mock from 'mockjs';
import { Random } from 'mockjs';
//手机号
// 创建自定义 Mock 函数
Random.extend({
    // 自定义函数名: function 函数  
    telFn: function() {    
      const phonePrefix = ['132', '135', '189','156','138','181','186']
      const index = Math.floor(Math.random() * phonePrefix.length)
      var phone = phonePrefix[index] + Mock.mock(/\d{8}/);
      return this.pick(phone)
  }
})
// var ids = Mock.mock({"id|1-300":1000})
let list = Mock.mock({
    "tableList|160-300":[
        {
            'id|1-899':3000,
            username:'@cname',
            "gender|0-1":100, // 0 女  1 男
            email:'@email',
            phone: '@telFn()',
            address:'@county(true)',//地址方法
        }
    ]
})
// 获取用户数据列表
export const AddressList = (data) =>request({
    url:'/AddressList',
    method:'get',
    data
})
Mock.mock(/\/AddressList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    let _list = list.tableList; //整体数据
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})
//删除数据
export const detAddress = (data) =>request({
    url:'/detAddress',
    mothed:'detAddress',
    data
})
Mock.mock(/\/detAddress/,config=>{
    let {id} = JSON.parse(config.body);
    let _list = list.tableList;
    if(id){
        _list.splice((_list.findIndex(item=>item.id==id)),1)
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})
// 修改数据
export const editAddress = (data) =>request({
    url:'/editAddress',
    mothed:'put',
    data
})
Mock.mock(/\/editAddress/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    let _list = list.tableList;
    if(id){
        if(datas.email || datas.phone){
            for(let item of _list){
                if(item.id==id){
                    item.email = datas.email?datas.email:item.email;
                    item.phone = datas.phone ? datas.phone : item.phone;
                    item.address = datas.address?datas.address:items.address;
                }
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

//添加用户
export const addAddress = (data) =>request({
    url:'/addAddress',
    mothed:'post',
    data
});
Mock.mock(/\/addAddress/,config=>{
    var ids = Mock.mock({"id|900-999":1000})
    let datas = JSON.parse(config.body)
    let {username,gender} = datas;
    let _list = list.tableList;
    if(username && gender){
        _list.unshift({
            id:ids.id,
            username,
            gender,
            email:datas.email?datas.email:'',
            phone:datas.phone?datas.phone:'',
            address:datas.address?datas.address:'',
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})
//查询数据 根据手机号 和用户名
export const queryAddress = (data) =>request({
    url:'/queryAddress',
    mothed:'get',
    data
})
Mock.mock(/\/queryAddress/,config=>{
    let datas = JSON.parse(config.body);
    let _list = list.tableList;
    let arr = [];
    if(datas.username || datas.phone){
        if(datas.username){
            arr = _list.filter((item,index)=>item.username.indexOf(datas.username)!=-1);
        }
        if(datas.phone){
            arr = (arr.length==0?_list:arr).filter((item,index)=>item.phone.indexOf(datas.phone)!=-1);
        }
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

